﻿package org.flixel
{
    import flash.display.*;
    import flash.geom.*;
    import org.flixel.plugin.*;
    import org.flixel.system.*;
    import org.flixel.system.input.*;

    public class FlxG extends Object
    {
        public static var LIBRARY_NAME:String = "flixel";
        public static var LIBRARY_MAJOR_VERSION:uint = 2;
        public static var LIBRARY_MINOR_VERSION:uint = 55;
        public static const DEBUGGER_STANDARD:uint = 0;
        public static const DEBUGGER_MICRO:uint = 1;
        public static const DEBUGGER_BIG:uint = 2;
        public static const DEBUGGER_TOP:uint = 3;
        public static const DEBUGGER_LEFT:uint = 4;
        public static const DEBUGGER_RIGHT:uint = 5;
        public static const RED:uint = 4.2949e+009;
        public static const GREEN:uint = 4.27825e+009;
        public static const BLUE:uint = 4.27823e+009;
        public static const PINK:uint = 4.29393e+009;
        public static const WHITE:uint = 4.29497e+009;
        public static const BLACK:uint = 4.27819e+009;
        static var _game:FlxGame;
        public static var paused:Boolean;
        public static var debug:Boolean;
        public static var elapsed:Number;
        public static var timeScale:Number;
        public static var width:uint;
        public static var height:uint;
        public static var worldBounds:FlxRect;
        public static var worldDivisions:uint;
        public static var visualDebug:Boolean;
        public static var mobile:Boolean;
        public static var globalSeed:Number;
        public static var levels:Array;
        public static var level:int;
        public static var scores:Array;
        public static var score:int;
        public static var saves:Array;
        public static var save:int;
        public static var mouse:Mouse;
        public static var keys:Keyboard;
        public static var music:FlxSound;
        public static var sounds:FlxGroup;
        public static var mute:Boolean;
        static var _volume:Number;
        public static var cameras:Array;
        public static var camera:FlxCamera;
        public static var useBufferLocking:Boolean;
        static var _cameraRect:Rectangle;
        public static var plugins:Array;
        public static var volumeHandler:Function;
        public static var flashGfxSprite:Sprite;
        public static var flashGfx:Graphics;
        static var _cache:Object;

        public function FlxG()
        {
            return;
        }// end function

        public static function getLibraryName() : String
        {
            return FlxG.LIBRARY_NAME + " v" + FlxG.LIBRARY_MAJOR_VERSION + "." + FlxG.LIBRARY_MINOR_VERSION;
        }// end function

        public static function log(param1:Object) : void
        {
            if (_game != null && _game._debugger != null)
            {
                _game._debugger.log.add(param1 == null ? ("ERROR: null object") : (param1.toString()));
            }
            return;
        }// end function

        public static function watch(param1:Object, param2:String, param3:String = null) : void
        {
            if (_game != null && _game._debugger != null)
            {
                _game._debugger.watch.add(param1, param2, param3);
            }
            return;
        }// end function

        public static function unwatch(param1:Object, param2:String = null) : void
        {
            if (_game != null && _game._debugger != null)
            {
                _game._debugger.watch.remove(param1, param2);
            }
            return;
        }// end function

        public static function get framerate() : Number
        {
            return 1000 / _game._step;
        }// end function

        public static function set framerate(param1:Number) : void
        {
            _game._step = 1000 / param1;
            if (_game._maxAccumulation < _game._step)
            {
                _game._maxAccumulation = _game._step;
            }
            return;
        }// end function

        public static function get flashFramerate() : Number
        {
            if (_game.root != null)
            {
                return _game.stage.frameRate;
            }
            return 0;
        }// end function

        public static function set flashFramerate(param1:Number) : void
        {
            _game._flashFramerate = param1;
            if (_game.root != null)
            {
                _game.stage.frameRate = _game._flashFramerate;
            }
            _game._maxAccumulation = 2000 / _game._flashFramerate - 1;
            if (_game._maxAccumulation < _game._step)
            {
                _game._maxAccumulation = _game._step;
            }
            return;
        }// end function

        public static function random() : Number
        {
            var _loc_1:* = FlxU.srand(globalSeed);
            globalSeed = FlxU.srand(globalSeed);
            return _loc_1;
        }// end function

        public static function shuffle(param1:Array, param2:uint) : Array
        {
            var _loc_4:uint = 0;
            var _loc_5:uint = 0;
            var _loc_6:Object = null;
            var _loc_3:uint = 0;
            while (_loc_3 < param2)
            {
                
                _loc_4 = FlxG.random() * param1.length;
                _loc_5 = FlxG.random() * param1.length;
                _loc_6 = param1[_loc_5];
                param1[_loc_5] = param1[_loc_4];
                param1[_loc_4] = _loc_6;
                _loc_3 = _loc_3 + 1;
            }
            return param1;
        }// end function

        public static function getRandom(param1:Array, param2:uint = 0, param3:uint = 0) : Object
        {
            var _loc_4:uint = 0;
            if (param1 != null)
            {
                _loc_4 = param3;
                if (_loc_4 == 0 || _loc_4 > param1.length - param2)
                {
                    _loc_4 = param1.length - param2;
                }
                if (_loc_4 > 0)
                {
                    return param1[param2 + uint(FlxG.random() * _loc_4)];
                }
            }
            return null;
        }// end function

        public static function loadReplay(param1:String, param2:FlxState = null, param3:Array = null, param4:Number = 0, param5:Function = null) : void
        {
            _game._replay.load(param1);
            if (param2 == null)
            {
                FlxG.resetGame();
            }
            else
            {
                FlxG.switchState(param2);
            }
            _game._replayCancelKeys = param3;
            _game._replayTimer = param4 * 1000;
            _game._replayCallback = param5;
            _game._replayRequested = true;
            return;
        }// end function

        public static function reloadReplay(param1:Boolean = true) : void
        {
            if (param1)
            {
                FlxG.resetGame();
            }
            else
            {
                FlxG.resetState();
            }
            if (_game._replay.frameCount > 0)
            {
                _game._replayRequested = true;
            }
            return;
        }// end function

        public static function stopReplay() : void
        {
            _game._replaying = false;
            if (_game._debugger != null)
            {
                _game._debugger.vcr.stopped();
            }
            resetInput();
            return;
        }// end function

        public static function recordReplay(param1:Boolean = true) : void
        {
            if (param1)
            {
                FlxG.resetGame();
            }
            else
            {
                FlxG.resetState();
            }
            _game._recordingRequested = true;
            return;
        }// end function

        public static function stopRecording() : String
        {
            _game._recording = false;
            if (_game._debugger != null)
            {
                _game._debugger.vcr.stopped();
            }
            return _game._replay.save();
        }// end function

        public static function resetState() : void
        {
            _game._requestedState = new FlxU.getClass(FlxU.getClassName(_game._state, false));
            return;
        }// end function

        public static function resetGame() : void
        {
            _game._requestedReset = true;
            return;
        }// end function

        public static function resetInput() : void
        {
            keys.reset();
            mouse.reset();
            return;
        }// end function

        public static function playMusic(param1:Class, param2:Number = 1) : void
        {
            if (music == null)
            {
                music = new FlxSound();
            }
            else if (music.active)
            {
                music.stop();
            }
            music.loadEmbedded(param1, true);
            music.volume = param2;
            music.survive = true;
            music.play();
            return;
        }// end function

        public static function loadSound(param1:Class = null, param2:Number = 1, param3:Boolean = false, param4:Boolean = false, param5:Boolean = false, param6:String = null) : FlxSound
        {
            if (param1 == null && param6 == null)
            {
                FlxG.log("WARNING: FlxG.loadSound() requires either\nan embedded sound or a URL to work.");
                return null;
            }
            var _loc_7:* = sounds.recycle(FlxSound) as FlxSound;
            if (param1 != null)
            {
                _loc_7.loadEmbedded(param1, param3, param4);
            }
            else
            {
                _loc_7.loadStream(param6, param3, param4);
            }
            _loc_7.volume = param2;
            if (param5)
            {
                _loc_7.play();
            }
            return _loc_7;
        }// end function

        public static function play(param1:Class, param2:Number = 1, param3:Boolean = false, param4:Boolean = true) : FlxSound
        {
            return FlxG.loadSound(param1, param2, param3, param4, true);
        }// end function

        public static function stream(param1:String, param2:Number = 1, param3:Boolean = false, param4:Boolean = true) : FlxSound
        {
            return FlxG.loadSound(null, param2, param3, param4, true, param1);
        }// end function

        public static function get volume() : Number
        {
            return _volume;
        }// end function

        public static function set volume(param1:Number) : void
        {
            _volume = param1;
            if (_volume < 0)
            {
                _volume = 0;
            }
            else if (_volume > 1)
            {
                _volume = 1;
            }
            if (volumeHandler != null)
            {
                volumeHandler(FlxG.mute ? (0) : (_volume));
            }
            return;
        }// end function

        static function destroySounds(param1:Boolean = false) : void
        {
            var _loc_3:FlxSound = null;
            if (music != null && (param1 || !music.survive))
            {
                music.destroy();
                music = null;
            }
            var _loc_2:uint = 0;
            var _loc_4:* = sounds.members.length;
            while (_loc_2 < _loc_4)
            {
                
                _loc_3 = sounds.members[_loc_2++] as FlxSound;
                if (_loc_3 != null && (param1 || !_loc_3.survive))
                {
                    _loc_3.destroy();
                }
            }
            return;
        }// end function

        static function updateSounds() : void
        {
            if (music != null && music.active)
            {
                music.update();
            }
            if (sounds != null && sounds.active)
            {
                sounds.update();
            }
            return;
        }// end function

        public static function pauseSounds() : void
        {
            var _loc_2:FlxSound = null;
            if (music != null && music.exists && music.active)
            {
                music.pause();
            }
            var _loc_1:uint = 0;
            var _loc_3:* = sounds.length;
            while (_loc_1 < _loc_3)
            {
                
                _loc_2 = sounds.members[_loc_1++] as FlxSound;
                if (_loc_2 != null && _loc_2.exists && _loc_2.active)
                {
                    _loc_2.pause();
                }
            }
            return;
        }// end function

        public static function resumeSounds() : void
        {
            var _loc_2:FlxSound = null;
            if (music != null && music.exists)
            {
                music.play();
            }
            var _loc_1:uint = 0;
            var _loc_3:* = sounds.length;
            while (_loc_1 < _loc_3)
            {
                
                _loc_2 = sounds.members[_loc_1++] as FlxSound;
                if (_loc_2 != null && _loc_2.exists)
                {
                    _loc_2.resume();
                }
            }
            return;
        }// end function

        public static function checkBitmapCache(param1:String) : Boolean
        {
            return _cache[param1] != undefined && _cache[param1] != null;
        }// end function

        public static function createBitmap(param1:uint, param2:uint, param3:uint, param4:Boolean = false, param5:String = null) : BitmapData
        {
            var _loc_6:uint = 0;
            var _loc_7:String = null;
            if (param5 == null)
            {
                param5 = param1 + "x" + param2 + ":" + param3;
                if (param4 && checkBitmapCache(param5))
                {
                    _loc_6 = 0;
                    do
                    {
                        
                        _loc_7 = param5 + _loc_6++;
                    }while (checkBitmapCache(_loc_7))
                    param5 = _loc_7;
                }
            }
            if (!checkBitmapCache(param5))
            {
                _cache[param5] = new BitmapData(param1, param2, true, param3);
            }
            return _cache[param5];
        }// end function

        public static function addBitmap(param1:Class, param2:Boolean = false, param3:Boolean = false, param4:String = null) : BitmapData
        {
            var _loc_7:uint = 0;
            var _loc_8:String = null;
            var _loc_9:BitmapData = null;
            var _loc_10:Matrix = null;
            var _loc_5:Boolean = false;
            if (param4 == null)
            {
                param4 = String(param1) + (param2 ? ("_REVERSE_") : (""));
                if (param3 && checkBitmapCache(param4))
                {
                    _loc_7 = 0;
                    do
                    {
                        
                        _loc_8 = param4 + _loc_7++;
                    }while (checkBitmapCache(_loc_8))
                    param4 = _loc_8;
                }
            }
            if (!checkBitmapCache(param4))
            {
                _cache[param4] = new param1.bitmapData;
                if (param2)
                {
                    _loc_5 = true;
                }
            }
            var _loc_6:* = _cache[param4];
            if (!_loc_5 && param2 && _loc_6.width == new param1.bitmapData.width)
            {
                _loc_5 = true;
            }
            if (_loc_5)
            {
                _loc_9 = new BitmapData(_loc_6.width << 1, _loc_6.height, true, 0);
                _loc_9.draw(_loc_6);
                _loc_10 = new Matrix();
                _loc_10.scale(-1, 1);
                _loc_10.translate(_loc_9.width, 0);
                _loc_9.draw(_loc_6, _loc_10);
                _loc_6 = _loc_9;
                _cache[param4] = _loc_6;
            }
            return _loc_6;
        }// end function

        public static function clearBitmapCache() : void
        {
            _cache = new Object();
            return;
        }// end function

        public static function get stage() : Stage
        {
            if (_game.root != null)
            {
                return _game.stage;
            }
            return null;
        }// end function

        public static function get state() : FlxState
        {
            return _game._state;
        }// end function

        public static function switchState(param1:FlxState) : void
        {
            _game._requestedState = param1;
            return;
        }// end function

        public static function setDebuggerLayout(param1:uint) : void
        {
            if (_game._debugger != null)
            {
                _game._debugger.setLayout(param1);
            }
            return;
        }// end function

        public static function resetDebuggerLayout() : void
        {
            if (_game._debugger != null)
            {
                _game._debugger.resetLayout();
            }
            return;
        }// end function

        public static function addCamera(param1:FlxCamera) : FlxCamera
        {
            FlxG._game.addChildAt(param1._flashSprite, FlxG._game.getChildIndex(FlxG._game._mouse));
            FlxG.cameras.push(param1);
            return param1;
        }// end function

        public static function removeCamera(param1:FlxCamera, param2:Boolean = true) : void
        {
            var Camera:* = param1;
            var Destroy:* = param2;
            try
            {
                FlxG._game.removeChild(Camera._flashSprite);
            }
            catch (E:Error)
            {
                FlxG.log("Error removing camera, not part of game.");
            }
            if (Destroy)
            {
                Camera.destroy();
            }
            return;
        }// end function

        public static function resetCameras(param1:FlxCamera = null) : void
        {
            var _loc_2:FlxCamera = null;
            var _loc_3:uint = 0;
            var _loc_4:* = cameras.length;
            while (_loc_3 < _loc_4)
            {
                
                _loc_2 = FlxG.cameras[_loc_3++] as FlxCamera;
                FlxG._game.removeChild(_loc_2._flashSprite);
                _loc_2.destroy();
            }
            FlxG.cameras.length = 0;
            if (param1 == null)
            {
                param1 = new FlxCamera(0, 0, FlxG.width, FlxG.height);
            }
            FlxG.camera = FlxG.addCamera(param1);
            return;
        }// end function

        public static function flash(param1:uint = 4.29497e+009, param2:Number = 1, param3:Function = null, param4:Boolean = false) : void
        {
            var _loc_5:uint = 0;
            var _loc_6:* = FlxG.cameras.length;
            while (_loc_5 < _loc_6)
            {
                
                (FlxG.cameras[_loc_5++] as FlxCamera).flash(param1, param2, param3, param4);
            }
            return;
        }// end function

        public static function fade(param1:uint = 4.27819e+009, param2:Number = 1, param3:Function = null, param4:Boolean = false) : void
        {
            var _loc_5:uint = 0;
            var _loc_6:* = FlxG.cameras.length;
            while (_loc_5 < _loc_6)
            {
                
                (FlxG.cameras[_loc_5++] as FlxCamera).fade(param1, param2, param3, param4);
            }
            return;
        }// end function

        public static function shake(param1:Number = 0.05, param2:Number = 0.5, param3:Function = null, param4:Boolean = true, param5:uint = 0) : void
        {
            var _loc_6:uint = 0;
            var _loc_7:* = FlxG.cameras.length;
            while (_loc_6 < _loc_7)
            {
                
                (FlxG.cameras[_loc_6++] as FlxCamera).shake(param1, param2, param3, param4, param5);
            }
            return;
        }// end function

        public static function get bgColor() : uint
        {
            if (FlxG.camera == null)
            {
                return 4278190080;
            }
            return FlxG.camera.bgColor;
        }// end function

        public static function set bgColor(param1:uint) : void
        {
            var _loc_2:uint = 0;
            var _loc_3:* = FlxG.cameras.length;
            while (_loc_2 < _loc_3)
            {
                
                (FlxG.cameras[_loc_2++] as FlxCamera).bgColor = param1;
            }
            return;
        }// end function

        public static function overlap(param1:FlxBasic = null, param2:FlxBasic = null, param3:Function = null, param4:Function = null) : Boolean
        {
            if (param1 == null)
            {
                param1 = FlxG.state;
            }
            if (param2 === param1)
            {
                param2 = null;
            }
            FlxQuadTree.divisions = FlxG.worldDivisions;
            var _loc_5:* = new FlxQuadTree(FlxG.worldBounds.x, FlxG.worldBounds.y, FlxG.worldBounds.width, FlxG.worldBounds.height);
            new FlxQuadTree(FlxG.worldBounds.x, FlxG.worldBounds.y, FlxG.worldBounds.width, FlxG.worldBounds.height).load(param1, param2, param3, param4);
            var _loc_6:* = _loc_5.execute();
            _loc_5.destroy();
            return _loc_6;
        }// end function

        public static function collide(param1:FlxBasic = null, param2:FlxBasic = null, param3:Function = null) : Boolean
        {
            return overlap(param1, param2, param3, FlxObject.separate);
        }// end function

        public static function addPlugin(param1:FlxBasic) : FlxBasic
        {
            var _loc_2:* = FlxG.plugins;
            var _loc_3:uint = 0;
            var _loc_4:* = _loc_2.length;
            while (_loc_3 < _loc_4)
            {
                
                if (_loc_2[_loc_3++].toString() == param1.toString())
                {
                    return param1;
                }
            }
            _loc_2.push(param1);
            return param1;
        }// end function

        public static function getPlugin(param1:Class) : FlxBasic
        {
            var _loc_2:* = FlxG.plugins;
            var _loc_3:uint = 0;
            var _loc_4:* = _loc_2.length;
            while (_loc_3 < _loc_4)
            {
                
                if (_loc_2[_loc_3] is param1)
                {
                    return plugins[_loc_3];
                }
                _loc_3 = _loc_3 + 1;
            }
            return null;
        }// end function

        public static function removePlugin(param1:FlxBasic) : FlxBasic
        {
            var _loc_2:* = FlxG.plugins;
            var _loc_3:* = _loc_2.length - 1;
            while (_loc_3 >= 0)
            {
                
                if (_loc_2[_loc_3] == param1)
                {
                    _loc_2.splice(_loc_3, 1);
                }
                _loc_3 = _loc_3 - 1;
            }
            return param1;
        }// end function

        public static function removePluginType(param1:Class) : Boolean
        {
            var _loc_2:Boolean = false;
            var _loc_3:* = FlxG.plugins;
            var _loc_4:* = _loc_3.length - 1;
            while (_loc_4 >= 0)
            {
                
                if (_loc_3[_loc_4] is param1)
                {
                    _loc_3.splice(_loc_4, 1);
                    _loc_2 = true;
                }
                _loc_4 = _loc_4 - 1;
            }
            return _loc_2;
        }// end function

        static function init(param1:FlxGame, param2:uint, param3:uint, param4:Number) : void
        {
            FlxG._game = param1;
            FlxG.width = param2;
            FlxG.height = param3;
            FlxG.mute = false;
            FlxG._volume = 0.5;
            FlxG.sounds = new FlxGroup();
            FlxG.volumeHandler = null;
            FlxG.clearBitmapCache();
            if (flashGfxSprite == null)
            {
                flashGfxSprite = new Sprite();
                flashGfx = flashGfxSprite.graphics;
            }
            FlxCamera.defaultZoom = param4;
            FlxG._cameraRect = new Rectangle();
            FlxG.cameras = new Array();
            useBufferLocking = false;
            plugins = new Array();
            addPlugin(new DebugPathDisplay());
            addPlugin(new TimerManager());
            FlxG.mouse = new Mouse(FlxG._game._mouse);
            FlxG.keys = new Keyboard();
            FlxG.mobile = false;
            FlxG.levels = new Array();
            FlxG.scores = new Array();
            FlxG.visualDebug = false;
            return;
        }// end function

        static function reset() : void
        {
            FlxG.clearBitmapCache();
            FlxG.resetInput();
            FlxG.destroySounds(true);
            FlxG.levels.length = 0;
            FlxG.scores.length = 0;
            FlxG.level = 0;
            FlxG.score = 0;
            FlxG.paused = false;
            FlxG.timeScale = 1;
            FlxG.elapsed = 0;
            FlxG.globalSeed = Math.random();
            FlxG.worldBounds = new FlxRect(-10, -10, FlxG.width + 20, FlxG.height + 20);
            FlxG.worldDivisions = 6;
            var _loc_1:* = FlxG.getPlugin(DebugPathDisplay) as DebugPathDisplay;
            if (_loc_1 != null)
            {
                _loc_1.clear();
            }
            return;
        }// end function

        static function updateInput() : void
        {
            FlxG.keys.update();
            if (!_game._debuggerUp || !_game._debugger.hasMouse)
            {
                FlxG.mouse.update(FlxG._game.mouseX, FlxG._game.mouseY);
            }
            return;
        }// end function

        static function lockCameras() : void
        {
            var _loc_1:FlxCamera = null;
            var _loc_2:* = FlxG.cameras;
            var _loc_3:uint = 0;
            var _loc_4:* = _loc_2.length;
            while (_loc_3 < _loc_4)
            {
                
                _loc_1 = _loc_2[_loc_3++] as FlxCamera;
                if (!(_loc_1 == null || !_loc_1.exists || !_loc_1.visible))
                {
                    if (useBufferLocking)
                    {
                        _loc_1.buffer.lock();
                    }
                    _loc_1.fill(_loc_1.bgColor);
                    _loc_1.screen.dirty = true;
                }
            }
            return;
        }// end function

        static function unlockCameras() : void
        {
            var _loc_1:FlxCamera = null;
            var _loc_2:* = FlxG.cameras;
            var _loc_3:uint = 0;
            var _loc_4:* = _loc_2.length;
            while (_loc_3 < _loc_4)
            {
                
                _loc_1 = _loc_2[_loc_3++] as FlxCamera;
                if (!(_loc_1 == null || !_loc_1.exists || !_loc_1.visible))
                {
                    _loc_1.drawFX();
                    if (useBufferLocking)
                    {
                        _loc_1.buffer.unlock();
                    }
                }
            }
            return;
        }// end function

        static function updateCameras() : void
        {
            var _loc_1:FlxCamera = null;
            var _loc_2:* = FlxG.cameras;
            var _loc_3:uint = 0;
            var _loc_4:* = _loc_2.length;
            while (_loc_3 < _loc_4)
            {
                
                _loc_1 = _loc_2[_loc_3++] as FlxCamera;
                if (_loc_1 != null && _loc_1.exists)
                {
                    if (_loc_1.active)
                    {
                        _loc_1.update();
                    }
                    _loc_1._flashSprite.x = _loc_1.x + _loc_1._flashOffsetX;
                    _loc_1._flashSprite.y = _loc_1.y + _loc_1._flashOffsetY;
                    _loc_1._flashSprite.visible = _loc_1.visible;
                }
            }
            return;
        }// end function

        static function updatePlugins() : void
        {
            var _loc_1:FlxBasic = null;
            var _loc_2:* = FlxG.plugins;
            var _loc_3:uint = 0;
            var _loc_4:* = _loc_2.length;
            while (_loc_3 < _loc_4)
            {
                
                _loc_1 = _loc_2[_loc_3++] as FlxBasic;
                if (_loc_1.exists && _loc_1.active)
                {
                    _loc_1.update();
                }
            }
            return;
        }// end function

        static function drawPlugins() : void
        {
            var _loc_1:FlxBasic = null;
            var _loc_2:* = FlxG.plugins;
            var _loc_3:uint = 0;
            var _loc_4:* = _loc_2.length;
            while (_loc_3 < _loc_4)
            {
                
                _loc_1 = _loc_2[_loc_3++] as FlxBasic;
                if (_loc_1.exists && _loc_1.visible)
                {
                    _loc_1.draw();
                }
            }
            return;
        }// end function

    }
}
